#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

const int n = 10;

void Worker(int number)
{
    int cnt = 10;
    while(cnt)
    {
        printf("I am child process, pid: %d, cnt: %d, number: %d\n", getpid(), cnt--, number);
        sleep(1);
    }
}

int main()
{
    for(int i = 0; i < 10; i++)
    {
        pid_t id = fork();
        if(id == 0)
        {
            Worker(i);
            exit(i);
        }
    }
    
    //等待多个子进程
    for(int i = 0; i < n; i++)
    {
        int status = 0;
        pid_t rid = waitpid(-1, NULL, 0);
        if(rid > 0)
        {
            printf("wait child %d success, exit code: %d\n", rid, WEXITSTATUS(status));
        }
    }
}
